package lesson03;

public class Test02 {
    /**
     * 添加元素到指定数组的末尾
     * @param ary 指定数组
     * @param value 要添加的元素
     * @return
     */
    public static int [] add(int [] ary,int value){
        int [] newAry = new int[ary.length + 1];
        for(int i = 0;i < ary.length;i++){
            newAry[i] = ary[i];
        }
        newAry[newAry.length - 1] = value;
        return newAry;
    }

    /**
     * 通过下标删除指定数组的元素
     * @param ary 指定数组
     * @param index 要删除元素的下标值
     * @return
     */
    public static int [] remove(int [] ary,int index){
        int [] newAry = new int[ary.length - 1];
        for(int i = 0;i < newAry.length;i++){
            if(i < index){
                newAry[i] = ary[i];
            }else{
                newAry[i] = ary[i+1];
            }
        }
        return newAry;
    }

    public static void main(String[] args) {
        int [] ary = {1,2,3,4,5,6,7,8};
        ary = add(ary,100);
        ary = remove(ary,3);
        ary = remove(ary,3);
        for (int i = 0;i < ary.length;i++){
            System.out.println(ary[i]);
        }
    }

}
